package com.rambler.common.weather.controller;

import com.rambler.api.common.vo.qweather.response.*;
import com.rambler.common.response.CommonResponse;
import com.rambler.common.weather.service.QWeatherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "天气接口", description = "对接和风天气 API 的天气数据服务")
@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Resource
    private QWeatherService qWeatherService;

    @Operation(summary = "获取实时天气")
    @GetMapping("/now")
    public CommonResponse<QWeatherNowResponse> getCurrentWeather(
            @Parameter(description = "城市名称", required = true)
            @RequestParam(name = "city") String city) {
        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }
        return qWeatherService.getNowWeather(city);
    }

    @Operation(summary = "获取未来天气（按天）")
    @GetMapping("/forecast/daily")
    public CommonResponse<QWeatherDailyForecastResponse> getDailyForecast(
            @Parameter(description = "城市名称", required = true)
            @RequestParam(name = "city") String city,
            @Parameter(description = "预报天数（3, 7, 10, 15, 30）", required = true)
            @RequestParam(name = "days") Integer days) {

        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }

        if (!isValidDays(days)) {
            return CommonResponse.failure("days 只支持 3, 7, 10, 15, 30");
        }

        CommonResponse<QWeatherDailyForecastResponse> result = qWeatherService.getWeatherDailyForecast(city, days);
        return handleResult(result);
    }

    @Operation(summary = "获取未来天气（按小时）")
    @GetMapping("/forecast/hourly")
    public CommonResponse<QWeatherHourlyForecastResponse> getHourlyForecast(
            @Parameter(description = "城市名称", required = true)
            @RequestParam(name = "city") String city,
            @Parameter(description = "预报小时数（24, 72, 168）", required = true)
            @RequestParam(name = "hours") Integer hours) {

        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }

        if (!isValidHours(hours)) {
            return CommonResponse.failure("hours 只支持 24, 72, 168");
        }

        CommonResponse<QWeatherHourlyForecastResponse> result = qWeatherService.getWeatherHourlyForecast(city, hours);
        return handleResult(result);
    }

    @Operation(summary = "获取未来2小时逐分钟降水")
    @GetMapping("/minutely-precipitation")
    public CommonResponse<QWeatherCommonResponse> getMinutelyPrecipitation(
            @Parameter(description = "城市名称", required = true)
            @RequestParam(name = "city") String city) {

        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }

        CommonResponse<QWeatherCommonResponse> result = qWeatherService.minutelyPrecipitation(city);
        return handleResult(result);
    }

    @Operation(summary = "获取天气预警信息")
    @GetMapping("/warning")
    public CommonResponse<QWeatherCommonResponse> getWeatherWarning(
            @Parameter(description = "城市名称", required = true)
            @RequestParam(name = "city") String city) {

        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }

        CommonResponse<QWeatherCommonResponse> result = qWeatherService.weatherWarning(city);
        return handleResult(result);
    }

    @Operation(summary = "城市信息查询")
    @GetMapping("/city-lookup")
    public CommonResponse<QWeatherLocationResponse> lookupCity(
            @Parameter(description = "城市名称，如：上海、shanghai", required = true)
            @RequestParam(name = "city") String city) {

        if (StringUtils.isBlank(city)) {
            return CommonResponse.failure("城市名称不能为空");
        }

        CommonResponse<QWeatherLocationResponse> result = qWeatherService.lookupCity(city);
        return handleResult(result);
    }

//    @Operation(summary = "获取生活指数")
//    @GetMapping("/life-index")
//    public CommonResponse<QWeatherIndicesResponse> getLifeIndices(
//            @Parameter(description = "城市名称", required = true)
//            @RequestParam(name = "city") String city,
//            @Parameter(description = "指数类型（可选，多个用逗号分隔）")
//            @RequestParam(name = "type", required = false) String type) {
//
//        if (StringUtils.isBlank(city)) {
//            return CommonResponse.failure("城市名称不能为空");
//        }
//
//        CommonResponse<QWeatherIndicesResponse> result = qWeatherService.getLifeIndices(city, type);
//        return handleResult(result);
//    }

    /**
     * 校验 days 是否是合法的值
     */
    private boolean isValidDays(Integer days) {
        return days == 3 || days == 7 || days == 10 || days == 15 || days == 30;
    }

    /**
     * 校验 hours 是否是合法的值
     */
    private boolean isValidHours(Integer hours) {
        return hours == 24 || hours == 72 || hours == 168;
    }

    /**
     * 通用返回值处理
     */
    private <T extends QWeatherCommonResponse> CommonResponse<T> handleResult(CommonResponse<T> response) {
        if (response == null || response.getData() == null) {
            return CommonResponse.failure("接口调用失败，返回为空");
        }

        T data = response.getData();
        if (!"200".equals(data.getCode())) {
            return CommonResponse.failure("和风天气返回错误码：" + data.getCode());
        }

        return CommonResponse.success(data);
    }
}
